/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.senac.academicnet.model;

import br.com.senac.academicnet.model.entidades.TurmaVO;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;

class TurmaDAO {

	private static Session session;
	
    static void incluir(TurmaVO turma) throws Exception {
    	DAOHibernate.incluir(turma);
    }

    static void alterar(TurmaVO turma) throws Exception {
    	DAOHibernate.alterar(turma);
    }

    static void deletar(TurmaVO turma) throws Exception {
    	DAOHibernate.deletar(turma);
    }
    
    static void deletarTudo() throws Exception {
    	List<TurmaVO> turmas = getTurmas();
    	
    	for(TurmaVO turma: turmas){
    		deletar(turma);
    	}
    }

    
    static List<TurmaVO> getTurmas() throws Exception {

        List<TurmaVO> turmas = new ArrayList<>();

        try {
			session = ConnectionHibernate.getSession();
			session.beginTransaction();
			turmas = (List<TurmaVO>) session.createCriteria(TurmaVO.class).list();
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
			throw e;
		}finally{
			session.close();
		}
        
        return turmas;
        
    }
    
    static TurmaVO getTurma(int codigo) throws Exception {
    	TurmaVO curso = new TurmaVO();

        try {
			session = ConnectionHibernate.getSession();
			session.beginTransaction();
			curso = (TurmaVO) session.get(TurmaVO.class, codigo);
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
			throw e;
		}finally{
			session.close();
		}
        
        return curso;
    }
}